﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using AspNetCs.Common;
using AspNetCs.Enumerations;
namespace AspNetCs.SQLServerDAL
{
    public class Point_Log_DAL : IPoint_Log
    {


        #region IPoint_Log 成员

        public int Add(Point_Log_Model md)
        {
            int result = 0;
            db.ParameterArry[] par = new db.ParameterArry[5];
            par[0].dbType = SqlDbType.Decimal;
            par[0].objValue = md.Point;
            par[0].parName = "@Point";

            par[1].dbType = SqlDbType.VarChar;
            par[1].objValue = md.NetIp;
            par[1].parName = "@netIP";

            par[2].dbType = SqlDbType.Int;
            par[2].objValue = md.UserId;
            par[2].parName = "@UserId";


            par[3].dbType = SqlDbType.VarChar;
            par[3].objValue = md.Readme;
            par[3].parName = "@Readme";

            par[4].dbType = SqlDbType.Int;
            par[4].objValue = md.UserType;
            par[4].parName = "@UserType";

            db Conn = new db();
            try
            {


                string Sqlstr = "begin tran ";
                if (md.UserType == (int)UserTypeEnum.Agent)
                {
                    if (md.UserId != 0)
                    {
                        Sqlstr += "  Update Agent Set Disu=Disu-(@Point)  where Id=@UserId ";
                    }
                    else
                    {
                        Sqlstr += "  Update Agent Set Disu=Disu-(@Point)";
                    }
                }
                else if (md.UserType == (int)UserTypeEnum.User)
                {
                    if (md.UserId != 0)
                    {
                        Sqlstr += "  Update Users Set userpoint=userpoint-(@Point)  where Userid=@UserId ";
                    }
                    else
                    {
                        Sqlstr += "  Update Users Set userpoint=userpoint-(@Point)";
                    }
                    
                }

                Sqlstr += "  insert into point_log(point,NetIp,UserId,Readme,UserType) values(@Point,@netIP,@UserId,@Readme,@UserType) ";
                //TODO:2009-10-22
                Sqlstr += " commit tran";

                result = Conn.ExcSql(Sqlstr, par);
            }
            catch
            {
                result = -1;

            }
            finally
            {
                Conn.close();
            }
            return result;
        }

        #endregion
    }
}
